package com.yx.cloud.animator;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.yx.cloud.R;

import java.util.ArrayList;

/**
 * Created by sane on 10/13/21.
 */
public class RestrictAreaClickView extends View implements View.OnTouchListener {
    public static final int VIEW_TYPE_YINGXING = 1;
    public static final int VIEW_TYPE_INXNI = 2;

    // 设置需要用到的变量
    public static final float RADIUS = 20f;// 圆的半径 = 20
    public static final float CLICK_RADIUS = 40f;// 点击效果圆的半径 = 40
    private Point currentPoint;// 当前点坐标
    private Paint mPaint;// 绘图画笔
    private Paint mClickPaint;//点击效果画笔
    private Paint mAreaPaint;//框框画笔
    private ArrayList<Point> points;
    public ImageView ivSave;
    public RelativeLayout rlArea;
    public RelativeLayout rlLine;
    public RelativeLayout rlModeSelect;
    public LinearLayout llSelectItem;
    public LinearLayout llNogo;
    public LinearLayout llNomop;
    public TextView tvRestrict;
    public TextView tvLine;
    public ImageView ivArea;
    public ImageView ivLine;
    public ImageView ivSelect;
    public  LinearLayout llBottom;

    private int currentAlpha = -1;
    private boolean flag = false;

    private RestrictRect noGoArea;
    private RestrictRect noMopArea;
    private RestrictRect noGoLine;

    protected int restrictStrokeColor = 0;
    protected int restrictFillColor = 0;
    protected int mopStrokeColor = 0;
    protected int mopFillColor = 0;

    protected Bitmap restrictDelete;
    protected Bitmap restrictRotate;
    protected Bitmap restrictDrag;
    protected Bitmap mopDelete;
    protected Bitmap mopRotate;
    protected Bitmap mopDrag;
    public int viewType = VIEW_TYPE_YINGXING;

    public boolean endAnimator = false;

    private Point areaPoint;
    private Point linePoint;

    // 构造方法(初始化画笔)
    public RestrictAreaClickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(getResources().getColor(R.color.click_circle_color));
        mClickPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mClickPaint.setColor(getResources().getColor(R.color.click_circle_color));
        // 初始化画笔
        mAreaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAreaPaint.setColor(restrictStrokeColor);

        noGoArea = new RestrictRect();
        noMopArea = new RestrictRect();
        noGoLine = new RestrictRect();

        restrictDelete = BitmapFactory.decodeResource(this.getResources(), R.mipmap.restrict_delete_help);
        restrictRotate = BitmapFactory.decodeResource(this.getResources(), R.mipmap.restrict_rotate_help);
        restrictDrag = BitmapFactory.decodeResource(this.getResources(), R.mipmap.restrict_drag_help);
        mopDelete = BitmapFactory.decodeResource(this.getResources(), R.mipmap.mop_delete_help);
        mopRotate = BitmapFactory.decodeResource(this.getResources(), R.mipmap.mop_rotate_help);
        mopDrag = BitmapFactory.decodeResource(this.getResources(), R.mipmap.mop_drag_help);

        restrictStrokeColor = getResources().getColor(R.color.restrict_area_stroke);
        restrictFillColor = getResources().getColor(R.color.restrict_area_fill);
        mopStrokeColor = getResources().getColor(R.color.restrict_area_mop_stroke);
        mopFillColor = getResources().getColor(R.color.restrict_area_mop_fill);
    }

    // 复写onDraw()从而实现绘制逻辑
    // 绘制逻辑:先在初始点画圆,通过监听当前坐标值(currentPoint)的变化,每次变化都调用onDraw()重新绘制圆,从而实现圆的平移动画效果
    @Override
    protected void onDraw(Canvas canvas) {
        if(ivSave.getX() != 0f && !flag) {
            flag = true;
            points = initPoints();
            startAnimation(points.get(8),points.get(1),500,500);
        }else {
            if(!flag){
                invalidate();
            }
        }
        // 如果当前点坐标为空(即第一次)
        if (currentPoint == null) {
            currentPoint = new Point(0, 0);
        } else {
            if(noGoArea.center != null){
                if(noGoArea.move){
                    noGoArea.center = currentPoint;
                    noGoArea.left = noGoArea.center.getX() - 150;
                    noGoArea.top =  noGoArea.center.getY() - 100;
                    noGoArea.right = noGoArea.center.getX() + 150;
                    noGoArea.bottom = noGoArea.center.getY() + 100;
                }
                if(noGoArea.drag){
                    noGoArea.right = currentPoint.getX();
                    noGoArea.bottom = currentPoint.getY();
                }

                if(noGoArea.center.getX() == points.get(9).getX() && noGoArea.center.getY() == points.get(9).getY()){
                    noGoArea.left = noGoArea.center.getX() - 150;
                    noGoArea.top =  noGoArea.center.getY() - 100;
                    noGoArea.right = noGoArea.center.getX() + 150;
                    noGoArea.bottom = noGoArea.center.getY() + 100;
                }

                mAreaPaint.setColor(restrictFillColor);
                mAreaPaint.setStyle(Paint.Style.FILL);
                //矩形填充
                canvas.drawRect(noGoArea.left, noGoArea.top, noGoArea.right, noGoArea.bottom, mAreaPaint);

                mAreaPaint.setColor(restrictStrokeColor);
                mAreaPaint.setStyle(Paint.Style.STROKE);
                //区域时虚线填充
                mAreaPaint.setPathEffect(new DashPathEffect(new float[]{10, 8}, 0));
                mAreaPaint.setStrokeWidth(3.0f);
                //矩形边框
                canvas.drawRect(noGoArea.left, noGoArea.top, noGoArea.right, noGoArea.bottom, mAreaPaint);

                if(!noGoArea.save){
                    //矩形点的图标
                    canvas.drawBitmap(restrictDelete, noGoArea.left - restrictDelete.getWidth() / 2, noGoArea.top - restrictDelete.getHeight() / 2, null);
                    canvas.drawBitmap(restrictRotate, noGoArea.right - restrictRotate.getWidth() / 2 , noGoArea.top - restrictRotate.getHeight() / 2 , null);
                    canvas.drawBitmap(restrictDrag, noGoArea.right - restrictDrag.getWidth() / 2, noGoArea.bottom - restrictDrag.getHeight() / 2, null);
                }
            }

            if(noGoLine.center != null){
                if(noGoLine.move){
                    noGoLine.center = currentPoint;
                    noGoLine.left = noGoLine.center.getX() - 120;
                    noGoLine.top =  noGoLine.center.getY();
                    noGoLine.right = noGoLine.center.getX() + 120;
                    noGoLine.bottom = noGoLine.center.getY();
                }

                if(noGoLine.drag){
                    noGoLine.right = currentPoint.getX();
                    noGoLine.bottom = currentPoint.getY();
                }

                if(noGoLine.center.getX() == points.get(11).getX() && noGoLine.center.getY() == points.get(11).getY()){
                    noGoLine.left = noGoLine.center.getX() - 120;
                    noGoLine.top = noGoLine.center.getY();
                    noGoLine.right = noGoLine.center.getX() + 120;
                    noGoLine.bottom = noGoLine.center.getY();
                }


                mAreaPaint.setColor(restrictStrokeColor);
                mAreaPaint.setStyle(Paint.Style.STROKE);
                //区域时虚线填充
                mAreaPaint.setPathEffect(new DashPathEffect(new float[]{10, 8}, 0));
                mAreaPaint.setStrokeWidth(3.0f);

                canvas.drawLine(noGoLine.left,noGoLine.top,noGoLine.right,noGoLine.bottom,mAreaPaint);

                if(!noGoLine.save){
                    canvas.drawBitmap(restrictDelete, noGoLine.left - restrictDelete.getWidth() / 2, noGoLine.top - restrictDelete.getHeight() / 2, null);
                    canvas.drawBitmap(restrictDrag, noGoLine.right - restrictDrag.getWidth() / 2 , noGoLine.bottom - restrictDrag.getHeight() / 2 , null);
                }
            }

            if(noMopArea.center != null){
                if(noMopArea.move){
                    noMopArea.center = currentPoint;
                    noMopArea.left = noMopArea.center.getX() - 150;
                    noMopArea.top =  noMopArea.center.getY() - 100;
                    noMopArea.right = noMopArea.center.getX() + 150;
                    noMopArea.bottom = noMopArea.center.getY() + 100;
                }
                if(noMopArea.drag){
                    noMopArea.right = currentPoint.getX();
                    noMopArea.bottom = currentPoint.getY();
                }

                if(noMopArea.center.getX() == points.get(13).getX() && noMopArea.center.getY() == points.get(13).getY()){
                    noMopArea.left = noMopArea.center.getX() - 150;
                    noMopArea.top =  noMopArea.center.getY() - 100;
                    noMopArea.right = noMopArea.center.getX() + 150;
                    noMopArea.bottom = noMopArea.center.getY() + 100;
                }

                mAreaPaint.setColor(mopFillColor);
                mAreaPaint.setStyle(Paint.Style.FILL);
                //矩形填充
                canvas.drawRect(noMopArea.left, noMopArea.top, noMopArea.right, noMopArea.bottom, mAreaPaint);

                mAreaPaint.setColor(mopStrokeColor);
                mAreaPaint.setStyle(Paint.Style.STROKE);
                //区域时虚线填充
                mAreaPaint.setPathEffect(new DashPathEffect(new float[]{10, 8}, 0));
                mAreaPaint.setStrokeWidth(3.0f);
                //矩形边框
                canvas.drawRect(noMopArea.left, noMopArea.top, noMopArea.right, noMopArea.bottom, mAreaPaint);

                if(!noMopArea.save){
                    //矩形点的图标
                    canvas.drawBitmap(mopDelete, noMopArea.left - mopDelete.getWidth() / 2, noMopArea.top - mopDelete.getHeight() / 2, null);
                    canvas.drawBitmap(mopRotate, noMopArea.right - mopRotate.getWidth() / 2 , noMopArea.top - mopRotate.getHeight() / 2 , null);
                    canvas.drawBitmap(mopDrag, noMopArea.right - mopDrag.getWidth() / 2, noMopArea.bottom - mopDrag.getHeight() / 2, null);
                }
            }

            if(viewType == VIEW_TYPE_YINGXING){
                mPaint.setColor(getResources().getColor(R.color.click_circle_color));
                mClickPaint.setColor(getResources().getColor(R.color.click_circle_color));
            }else {
                mPaint.setColor(getResources().getColor(R.color.button_color2));
                mClickPaint.setColor(getResources().getColor(R.color.button_color2));
            }

            float x = currentPoint.getX();
            float y = currentPoint.getY();
            if(x != 0 && y != 0){
                mPaint.setAlpha(204);
                canvas.drawCircle(x, y, RADIUS, mPaint);
                if(currentAlpha != -1){
                    mClickPaint.setAlpha(currentAlpha);
                    canvas.drawCircle(x, y, CLICK_RADIUS, mClickPaint);
                }
            }
        }
    }

    private ArrayList<Point> initPoints(){
        int[] location = new int[2];
        ArrayList<Point> points = new ArrayList<>();

        ivSave.getLocationInWindow(location);
        Point savePoint = new Point(location[0] + ivSave.getWidth() / 2f,location[1] + ivSave.getHeight() / 2f);
        points.add(savePoint);

        ivArea.getLocationInWindow(location);
        areaPoint = new Point(location[0] + ivArea.getWidth() / 2f,location[1] + ivArea.getHeight() / 2f);
        points.add(areaPoint);

        ivLine.getLocationInWindow(location);
        linePoint = new Point(location[0] + ivLine.getWidth() / 2f,location[1] + ivLine.getHeight() / 2f);
        points.add(linePoint);

        llNomop.getLocationInWindow(location);
        Point noMopPoint = new Point(location[0] + llNomop.getWidth() / 2f,location[1] + llNomop.getHeight() / 2f);
        points.add(noMopPoint);

        llNogo.getLocationInWindow(location);
        Point nogoPoint = new Point(location[0] + llNogo.getWidth() / 2f,location[1] + llNogo.getHeight() / 2f);
        points.add(nogoPoint);

        llSelectItem.getLocationInWindow(location);
        Point selectItemPoint = new Point(location[0] + llSelectItem.getWidth() / 2f,location[1] + llSelectItem.getHeight() / 2f);
        points.add(selectItemPoint);

        ivSelect.getLocationInWindow(location);
        Point modeSelectPoint = new Point(location[0] + ivSelect.getWidth() / 2f,location[1] + ivSelect.getHeight() / 2f);
        points.add(modeSelectPoint);

        tvRestrict.getLocationInWindow(location);
        Point tvRestrictPoint = new Point(location[0] + tvRestrict.getWidth() / 2f,location[1] + tvRestrict.getHeight() / 2f);
        points.add(tvRestrictPoint);

        rlArea.getLocationInWindow(location);
        Point firstPoint = new Point(location[0] + rlArea.getWidth(),location[1] + rlArea.getHeight());
        points.add(firstPoint);

        llBottom.getLocationInWindow(location);

        Point point9 = new Point(location[0] + 500,location[1] - 200);
        points.add(point9);//禁区初始位置中心点
        Point point10 = new Point(point9.getX() - 200,point9.getY() - 370);
        points.add(point10);//禁区移动后位置中心点

       Point point11 = new Point(location[0] + 450,location[1] - 250);
        points.add(point11);//禁过线初始位置中心点
        Point point12 = new Point(point9.getX() - 150,point9.getY() - 170);
        points.add(point12);//禁过线移动后位置中心点

        Point point13 = new Point(location[0] + 500,location[1] - 370);
        points.add(point13);//禁拖初始位置中心点
        Point point14 = new Point(point9.getX() - 200,location[1] - 200);
        points.add(point14);//禁拖移动后位置中心点

        /*
        com.yx.b34.animator.Point point9 = new com.yx.b34.animator.Point(600,1000);
        points.add(point9);//禁区初始位置中心点
        com.yx.b34.animator.Point point10 = new com.yx.b34.animator.Point(350,550);
        points.add(point10);//禁区移动后位置中心点

        com.yx.b34.animator.Point point11 = new com.yx.b34.animator.Point(600,950);
        points.add(point11);//禁过线初始位置中心点
        com.yx.b34.animator.Point point12 = new com.yx.b34.animator.Point(500,800);
        points.add(point12);//禁过线移动后位置中心点

        com.yx.b34.animator.Point point13 = new com.yx.b34.animator.Point(600,930);
        points.add(point13);//禁拖初始位置中心点
        com.yx.b34.animator.Point point14 = new com.yx.b34.animator.Point(350,1000);
        points.add(point14);//禁拖移动后位置中心点*/
        return points;
    }

    void startAnimation(Point start,Point end,int duration,int startDelay){

        // 步骤2:创建动画对象 & 设置初始值 和 结束值
        ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), start,end);
        // 参数说明
        // 参数1：TypeEvaluator 类型参数 - 使用自定义的PointEvaluator(实现了TypeEvaluator接口)
        // 参数2：初始动画的对象点
        // 参数3：结束动画的对象点

        // 步骤3：设置动画参数
        anim.setDuration(duration);
        anim.setRepeatCount(0);
        anim.setStartDelay(startDelay);
        // 设置动画时长

        // 步骤3：通过 值 的更新监听器，将改变的对象手动赋值给当前对象
        // 此处是将 改变后的坐标值对象 赋给 当前的坐标值对象
        // 设置 值的更新监听器
        // 即每当坐标值（Point对象）更新一次,该方法就会被调用一次
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentPoint = (Point) animation.getAnimatedValue();
                // 将每次变化后的坐标值（估值器PointEvaluator中evaluate（）返回的Piont对象值）到当前坐标值对象（currentPoint）
                // 从而更新当前坐标值（currentPoint）
                if(currentPoint.getX() == points.get(10).getX() && currentPoint.getY() == points.get(10).getY()){
                    currentAlpha = -1;
                    Point point15 = new Point(currentPoint.getX() + 150,currentPoint.getY() + 100);
                    if(points.get(points.size() - 1).getX() != point15.getX() || points.get(points.size() - 1).getY() != point15.getY()){
                        points.add(point15);//禁区移动后右下角坐标
                        Log.d("animator","add point15");
                    }
                }
                if(points.size() >= 16 && currentPoint.getX() == points.get(15).getX() && currentPoint.getY() == points.get(15).getY()){
                    Point point16 = new Point(currentPoint.getX() + 60,currentPoint.getY() + 40);
                    if(points.get(points.size() - 1).getX() != point16.getX() || points.get(points.size() - 1).getY() != point16.getY()){
                        points.add(point16);////禁区放大后右下角坐标
                        Log.d("animator","add point16");
                    }
                }
                if(currentPoint.getX() == points.get(12).getX() && currentPoint.getY() == points.get(12).getY()){
                    currentAlpha = -1;
                    Point point17 = new Point(currentPoint.getX() + 120,currentPoint.getY());
                    if(points.get(points.size() - 1).getX() != point17.getX() || points.get(points.size() - 1).getY() != point17.getY()){
                        points.add(point17);////禁过线移动后右下角坐标
                        Log.d("animator","add point17");
                    }
                }
                if(points.size() >= 18 && currentPoint.getX() == points.get(17).getX() && currentPoint.getY() == points.get(17).getY()){
                    Point point18 = new Point(currentPoint.getX() + 120,currentPoint.getY());
                    if(points.get(points.size() - 1).getX() != point18.getX() || points.get(points.size() - 1).getY() != point18.getY()){
                        points.add(point18);////禁过线放大后右下角坐标
                        Log.d("animator","add point18");
                    }
                }
                if(currentPoint.getX() == points.get(14).getX() && currentPoint.getY() == points.get(14).getY()){
                    currentAlpha = -1;
                    Point point19 = new Point(currentPoint.getX() + 150,currentPoint.getY() + 100);
                    if(points.get(points.size() - 1).getX() != point19.getX() || points.get(points.size() - 1).getY() != point19.getY()){
                        points.add(point19);////禁拖移动后右下角坐标
                        Log.d("animator","add point17");
                    }
                }
                if(points.size() >= 20 && currentPoint.getX() == points.get(19).getX() && currentPoint.getY() == points.get(19).getY()){
                    Point point20 = new Point(currentPoint.getX() - 60,currentPoint.getY() - 40);
                    if(points.get(points.size() - 1).getX() != point20.getX() || points.get(points.size() - 1).getY() != point20.getY()){
                        points.add(point20);////禁拖缩小后右下角坐标
                        Log.d("animator","add point18");
                    }
                }

                // 步骤4：每次赋值后就重新绘制，从而实现动画效果
                invalidate();
                // 调用invalidate()后,就会刷新View,即才能看到重新绘制的界面,即onDraw()会被重新调用一次
                // 所以坐标值每改变一次,就会调用onDraw()一次
            }
        });

        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                if(currentPoint != null){
                    if(currentPoint.getX() == points.get(9).getX() && currentPoint.getY() == points.get(9).getY()){
                        currentAlpha = 127;
                    }
                    if(currentPoint.getX() == points.get(10).getX() && currentPoint.getY() == points.get(10).getY()){
                        currentAlpha = -1;
                    }
                    if(currentPoint.getX() == points.get(0).getX() && currentPoint.getY() == points.get(0).getY()){
                        currentAlpha = -1;
                    }
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if(endAnimator){
                    return;
                }

                //inxni没有禁拖动画
                if(points.size() >= 19 && currentPoint.getX() == points.get(18).getX() + 100 && currentPoint.getY() == points.get(18).getY() + 200){
                    noMopArea.clear();
                    noGoArea.clear();
                    noGoLine.clear();
                    rlLine.setVisibility(VISIBLE);
                    ivLine.setEnabled(true);
                    tvLine.setEnabled(true);
                    tvRestrict.setText(R.string.MM_NoGo);
                    startAnimation(points.get(8),points.get(1),500,1000);
                    return;
                }

                if(currentPoint.getX() == points.get(1).getX() && currentPoint.getY() == points.get(1).getY()){
                    startAlphaAnimation(200);
                    startAnimation(points.get(1),points.get(9),500,200);
                } else if(currentPoint.getX() == points.get(9).getX() && currentPoint.getY() == points.get(9).getY()){
                    startAlphaAnimation(200);
                    noGoArea.move = true;
                    startAnimation(points.get(9),points.get(10),800,200);
                } else if(currentPoint.getX() == points.get(10).getX() && currentPoint.getY() == points.get(10).getY()){
                    currentAlpha = -1;
                    noGoArea.move = false;
                    startAnimation(points.get(10),points.get(15),300,0);
                }else if(currentPoint.getX() == points.get(15).getX() && currentPoint.getY() == points.get(15).getY()){
                    startAlphaAnimation(200);
                    noGoArea.move = false;
                    noGoArea.drag = true;
                    Log.d("animator","add point15 to point16");
                    startAnimation(points.get(15),points.get(16),500,0);
                }else if(currentPoint.getX() == points.get(16).getX() && currentPoint.getY() == points.get(16).getY()){
                    noGoArea.move = false;
                    noGoArea.drag = false;
                    currentAlpha = -1;
                    Log.d("animator","add point15 to point2");
                    startAnimation(points.get(16),points.get(2),500,0);
                } else if(currentPoint.getX() == points.get(2).getX() && currentPoint.getY() == points.get(2).getY()){
                    startAlphaAnimation(200);
                    startAnimation(points.get(2),points.get(11),500,200);
                }else if(currentPoint.getX() == points.get(11).getX() && currentPoint.getY() == points.get(11).getY()){
                    startAlphaAnimation(200);
                    noGoLine.move = true;
                    startAnimation(points.get(11),points.get(12),500,200);
                }else if(currentPoint.getX() == points.get(12).getX() && currentPoint.getY() == points.get(12).getY()){
                    currentAlpha = -1;
                    noGoLine.move = false;
                    startAnimation(points.get(12),points.get(17),300,0);
                }else if(currentPoint.getX() == points.get(17).getX() && currentPoint.getY() == points.get(17).getY()){
                    startAlphaAnimation(200);
                    noGoLine.move = false;
                    noGoLine.drag = true;
                    Log.d("animator","add point17 to point18");
                    startAnimation(points.get(17),points.get(18),500,0);
                }else if(currentPoint.getX() == points.get(18).getX() && currentPoint.getY() == points.get(18).getY()){
                    noGoLine.move = false;
                    noGoLine.drag = false;
                    currentAlpha = -1;
                    if(VIEW_TYPE_YINGXING == viewType || VIEW_TYPE_INXNI == viewType){
                        Log.d("animator","add point18 to point6");
                        startAnimation(points.get(18),points.get(6),500,0);
                    }else {
                        Log.d("animator","add point18 to end");
                        startAnimation(points.get(18),new Point(points.get(18).getX() + 100,points.get(18).getY() + 200),500,0);
                    }
                }else if(currentPoint.getX() == points.get(6).getX() && currentPoint.getY() == points.get(6).getY()){
                    llSelectItem.setVisibility(VISIBLE);
                    startAlphaAnimation(200);
                    startAnimation(points.get(6),points.get(3),500,200);
                }else if(currentPoint.getX() == points.get(3).getX() && currentPoint.getY() == points.get(3).getY()){
                    startAlphaAnimation(200);
                    llSelectItem.setVisibility(INVISIBLE);
                    rlLine.setVisibility(GONE);
                    ivLine.setEnabled(false);
                    tvLine.setEnabled(false);
                    tvRestrict.setText(R.string.MM_NoMop);
                    Point end = new Point(areaPoint.getX() + (linePoint.getX() - areaPoint.getX() ) / 2f,areaPoint.getY());
                    startAnimation(points.get(3),end,600,200);
                }else if(currentPoint.getX() == areaPoint.getX() + (linePoint.getX() - areaPoint.getX() ) / 2f && currentPoint.getY() == areaPoint.getY()){
                    startAlphaAnimation(200);
                    Point start = new Point(areaPoint.getX() + (linePoint.getX() - areaPoint.getX() ) / 2f,areaPoint.getY());
                    startAnimation(start,points.get(13),500,200);
                }else if(currentPoint.getX() == points.get(13).getX() && currentPoint.getY() == points.get(13).getY()){
                    startAlphaAnimation(200);
                    noMopArea.move = true;
                    startAnimation(points.get(13),points.get(14),500,200);
                }else if(currentPoint.getX() == points.get(14).getX() && currentPoint.getY() == points.get(14).getY()){
                    currentAlpha = -1;
                    noMopArea.move = false;
                    startAnimation(points.get(14),points.get(19),300,0);
                }else if(currentPoint.getX() == points.get(19).getX() && currentPoint.getY() == points.get(19).getY()){
                    startAlphaAnimation(200);
                    noMopArea.move = false;
                    noMopArea.drag = true;
                    Log.d("animator","add point19 to point20");
                    startAnimation(points.get(19),points.get(20),500,0);
                }else if(currentPoint.getX() == points.get(20).getX() && currentPoint.getY() == points.get(20).getY()){
                    noMopArea.move = false;
                    noMopArea.drag = false;
                    currentAlpha = -1;
                    Log.d("animator","add point20 to point0");
                    startAnimation(points.get(20),points.get(0),500,0);
                }else if(currentPoint.getX() == points.get(0).getX() && currentPoint.getY() == points.get(0).getY()){
                    startAlphaAnimation(200);
                    Point endPoint = new Point((points.get(0).getX() + 200),600);
                    startAnimation(points.get(0),endPoint,500,200);
                }else if(currentPoint.getX() == points.get(0).getX() + 200 && currentPoint.getY() == 600){
                    noMopArea.clear();
                    noGoArea.clear();
                    noGoLine.clear();
                    rlLine.setVisibility(VISIBLE);
                    ivLine.setEnabled(true);
                    tvLine.setEnabled(true);
                    tvRestrict.setText(R.string.MM_NoGo);
                    startAnimation(points.get(8),points.get(1),500,1000);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        // 启动动画
        anim.start();
    }

    void startAlphaAnimation(int duration){

        // 步骤2:创建动画对象 & 设置初始值 和 结束值
        ValueAnimator anim = ValueAnimator.ofInt(0,127);
        // 参数说明
        // 参数1：TypeEvaluator 类型参数 - 使用自定义的PointEvaluator(实现了TypeEvaluator接口)
        // 参数2：初始动画的对象点
        // 参数3：结束动画的对象点

        // 步骤3：设置动画参数
        anim.setDuration(duration);
        anim.setRepeatCount(0);
        // 设置动画时长

        // 步骤3：通过 值 的更新监听器，将改变的对象手动赋值给当前对象
        // 此处是将 改变后的坐标值对象 赋给 当前的坐标值对象
        // 设置 值的更新监听器
        // 即每当坐标值（Point对象）更新一次,该方法就会被调用一次
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentAlpha = (int) animation.getAnimatedValue();
                // 将每次变化后的坐标值（估值器PointEvaluator中evaluate（）返回的Piont对象值）到当前坐标值对象（currentPoint）
                // 从而更新当前坐标值（currentPoint）

                // 步骤4：每次赋值后就重新绘制，从而实现动画效果
                invalidate();
                // 调用invalidate()后,就会刷新View,即才能看到重新绘制的界面,即onDraw()会被重新调用一次
                // 所以坐标值每改变一次,就会调用onDraw()一次
            }
        });

        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                currentAlpha = -1;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
               if(currentPoint.getX() == points.get(1).getX() && currentPoint.getY() == points.get(1).getY()){
                    currentAlpha = -1;
                    noGoArea.center = points.get(9);
                }
                if(currentPoint.getX() == points.get(2).getX() && currentPoint.getY() == points.get(2).getY()){
                    currentAlpha = -1;
                    noGoLine.center = points.get(11);
                    noGoArea.save = true;
                }
                if(currentPoint.getX() == points.get(6).getX() && currentPoint.getY() == points.get(6).getY()){
                    currentAlpha = -1;
                }
                if(currentPoint.getX() == points.get(3).getX() && currentPoint.getY() == points.get(3).getY()){
                    currentAlpha = -1;
                }
                if(currentPoint.getX() == areaPoint.getX() + (linePoint.getX() - areaPoint.getX() ) / 2f && currentPoint.getY() == areaPoint.getY()){
                    currentAlpha = -1;
                    noMopArea.center = points.get(13);
                    noGoLine.save = true;
                }
                if(currentPoint.getX() == points.get(0).getX() && currentPoint.getY() == points.get(0).getY()){
                    currentAlpha = -1;
                    noMopArea.save = true;
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        // 启动动画
        anim.start();
    }

    public ArrayList<Point> getPoints() {
        return points;
    }

    public void setPoints(ArrayList<Point> points) {
        this.points = points;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
}

